﻿using System;

namespace DotNetImage.Imaging.Filters
{
    /// <summary>
    /// This one works by working out the greatest difference between pixels in 3x3 area, using "cross" approach.
    /// Calculation is made per color.
    /// d1 = ABS(m11 - m33)
    /// d1 = ABS(m13 - m31)
    /// d1 = ABS(m12 - m32)
    /// d1 = ABS(m21 - m23)
    /// p = MAX(d1, d2, d3, d4)
    /// </summary>
    public class EdgeDetectEnhanceFilter : Conv3x3
    {
        [ParameterAttribute(Name = "Treshold", DefaultValue = 0, MinValue = 0, MaxValue = 255, Step = 1)]
        public byte Treshold { get; set; }

        [ParameterAttribute(Name = "Edge Detection Type", DefaultValue = (int)EdgeDetectionType.EDGE_DETECT_ENHANCE)]
        public EdgeDetectionType DetectionType { get { return EdgeDetectionType.EDGE_DETECT_ENHANCE; } }

        protected override void PrepareData(DotNetImage bmp)
        {
            base.PrepareData(bmp);
            bmp.CreatePixelCopy();
            startX = 1;
            startY = 1;
        }

        protected override void ProcessPixel(ref Pixel pix, uint x = 0, uint y = 0)
        {
            Pixel pix13_31 = Subject.GetPreservedPixel(x + 1, y - 1); pix13_31.SubtractAbs(Subject.GetPreservedPixel(x - 1, y + 1));
            Pixel pix11_33 = Subject.GetPreservedPixel(x + 1, y + 1); pix11_33.SubtractAbs(Subject.GetPreservedPixel(x - 1, y - 1));
            Pixel pix12_32 = Subject.GetPreservedPixel(x, y - 1); pix12_32 .SubtractAbs(Subject.GetPreservedPixel(x, y + 1));
            Pixel pix21_23 = Subject.GetPreservedPixel(x - 1, y); pix21_23.SubtractAbs(Subject.GetPreservedPixel(x + 1, y));
            Pixel pixMax = Pixel.GetMax(Treshold, pix13_31, pix11_33, pix12_32, pix21_23);
            pix = Pixel.GetMax(0, pixMax, pix);
        }
    }
}
